<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.5">
<TITLE>czestmyr.jjsched.Connector (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">Connector.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="MultiLineComment">/*  This file is a part of JJSched source code
<A NAME="2"></A>    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
<A NAME="3"></A>    Copyright (C) 2008 Cestmir "CZestmyr" Houska
<A NAME="4"></A>
<A NAME="5"></A>    This program is free software: you can redistribute it and/or modify
<A NAME="6"></A>    it under the terms of the GNU General Public License as published by
<A NAME="7"></A>    the Free Software Foundation, either version 3 of the License, or
<A NAME="8"></A>    (at your option) any later version.
<A NAME="9"></A>
<A NAME="10"></A>    This program is distributed in the hope that it will be useful,
<A NAME="11"></A>    but WITHOUT ANY WARRANTY; without even the implied warranty of
<A NAME="12"></A>    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
<A NAME="13"></A>    GNU General Public License for more details.
<A NAME="14"></A>
<A NAME="15"></A>    You should have received a copy of the GNU General Public License
<A NAME="16"></A>    along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt; */</FONT>
<A NAME="17"></A>
<A NAME="18"></A><FONT ID="Package">package</FONT> czestmyr.jjsched;
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="Import">import</FONT> java.lang.*;
<A NAME="21"></A><FONT ID="Import">import</FONT> java.nio.charset.*;
<A NAME="22"></A><FONT ID="Import">import</FONT> java.nio.*;
<A NAME="23"></A>
<A NAME="24"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.*;
<A NAME="25"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.packet.*;
<A NAME="26"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.filter.*;
<A NAME="27"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.util.*;
<A NAME="28"></A>
<A NAME="29"></A><FONT ID="FormalComment">/**
<A NAME="30"></A> * Handles the connection to the XMPP server.
<A NAME="31"></A> */</FONT>
<A NAME="32"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> Connector{
<A NAME="33"></A>    <FONT ID="FormalComment">/**
<A NAME="34"></A>     * Constructor.
<A NAME="35"></A>     *
<A NAME="36"></A>     * @param   conn    Information about the connection to the XMPP server
<A NAME="37"></A>     */</FONT>
<A NAME="38"></A>    Connector(XMPPConnection conn, <A HREF="../../czestmyr/jjsched/DatabaseConnector.java.html">DatabaseConnector</A> dbc){
<A NAME="39"></A>        connection = conn;
<A NAME="40"></A>        theDBConnector = dbc;
<A NAME="41"></A>    }
<A NAME="42"></A>
<A NAME="43"></A>    <FONT ID="FormalComment">/**
<A NAME="44"></A>     * Tries to connect to the XMPP server
<A NAME="45"></A>     * @throws  Exception   In case of failed attempt to connect to the XMPP server
<A NAME="46"></A>     */</FONT>
<A NAME="47"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> connect() <FONT ID="Throws">throws</FONT> Exception{
<A NAME="48"></A>        connection.connect();
<A NAME="49"></A>    }
<A NAME="50"></A>
<A NAME="51"></A>    <FONT ID="FormalComment">/**
<A NAME="52"></A>     * Disconnects from the XMPP server
<A NAME="53"></A>     */</FONT>
<A NAME="54"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> disconnect(){
<A NAME="55"></A>        connection.disconnect();
<A NAME="56"></A>    }
<A NAME="57"></A>
<A NAME="58"></A>    <FONT ID="FormalComment">/**
<A NAME="59"></A>     * Tries to login to the XMPP server with the given username and password.
<A NAME="60"></A>     *
<A NAME="61"></A>     * @param   userName    The username of the user to login
<A NAME="62"></A>     * @param   password    The password of the given user
<A NAME="63"></A>     * @throws  Exception   In case of unsuccessful login
<A NAME="64"></A>     */</FONT>
<A NAME="65"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> login(String userName, String password) <FONT ID="Throws">throws</FONT> Exception{
<A NAME="66"></A>        connection.login(userName, password);
<A NAME="67"></A>
<A NAME="68"></A>        userWithoutResource = StringUtils.parseName(connection.getUser()) + <FONT ID="StringLiteral">"@"</FONT> +
<A NAME="69"></A>                              StringUtils.parseServer(connection.getUser());
<A NAME="70"></A>    }
<A NAME="71"></A>
<A NAME="72"></A>    <FONT ID="FormalComment">/**
<A NAME="73"></A>     * Sends a message through the established XMPP connection to the given user.
<A NAME="74"></A>     * The user has to be specified by his/her Jabber ID.
<A NAME="75"></A>     *
<A NAME="76"></A>     * @param   message The message to be sent
<A NAME="77"></A>     * @param   user    Jabber ID of the recipient of the message. Can be "console" to output via console
<A NAME="78"></A>     */</FONT>
<A NAME="79"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> sendMessage(String message, String user){
<A NAME="80"></A>        <FONT ID="If">if</FONT> (user.equals(<FONT ID="StringLiteral">"console"</FONT>)) {
<A NAME="81"></A>            System.out.println(message);
<A NAME="82"></A>            <FONT ID="Return">return</FONT>;
<A NAME="83"></A>        }
<A NAME="84"></A>
<A NAME="85"></A>        <FONT ID="SingleLineComment">//Debug
<A NAME="86"></A></FONT>        System.out.println(<FONT ID="StringLiteral">"sending message"</FONT>);
<A NAME="87"></A>        System.out.println(message);
<A NAME="88"></A>
<A NAME="89"></A>        <FONT ID="SingleLineComment">//Try to encode the message to the user's preffered charset
<A NAME="90"></A></FONT>        <FONT ID="Try">try</FONT> {   
<A NAME="91"></A>            <FONT ID="SingleLineComment">//String encoding = theDBConnector.getUserCharset(user);
<A NAME="92"></A></FONT>            <FONT ID="SingleLineComment">//message = Charset.forName(encoding).encode(ByteBuffer.wrap(message.getBytes(encoding)).asCharBuffer()).toString();
<A NAME="93"></A></FONT>        } <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="94"></A>            <FONT ID="SingleLineComment">//Do nothing - the message will remain in UTF-8
<A NAME="95"></A></FONT>            <FONT ID="SingleLineComment">//DEBUG: print the exception
<A NAME="96"></A></FONT>            System.out.println(e.getMessage());
<A NAME="97"></A>        }
<A NAME="98"></A>
<A NAME="99"></A>        <FONT ID="SingleLineComment">//Debug
<A NAME="100"></A></FONT>        System.out.println(<FONT ID="StringLiteral">"encoded message"</FONT>);
<A NAME="101"></A>        System.out.println(message);
<A NAME="102"></A>
<A NAME="103"></A>        Message m = <FONT ID="New">new</FONT> Message(user, Message.Type.chat);
<A NAME="104"></A>        m.setBody(message);
<A NAME="105"></A>        m.setFrom(userWithoutResource);
<A NAME="106"></A>        connection.sendPacket(m);
<A NAME="107"></A>    }
<A NAME="108"></A>
<A NAME="109"></A>    <FONT ID="FormalComment">/**
<A NAME="110"></A>     * Adds a message listener to the underlying XMPP connection.
<A NAME="111"></A>     *
<A NAME="112"></A>     * @param   p   The packet listener that will be notified of an incoming message
<A NAME="113"></A>     */</FONT>
<A NAME="114"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> addMessageListener(PacketListener p){
<A NAME="115"></A>        connection.addPacketListener(p, <FONT ID="New">new</FONT> PacketTypeFilter(Message.<FONT ID="Class">class</FONT>));
<A NAME="116"></A>    }
<A NAME="117"></A>
<A NAME="118"></A>    <FONT ID="FormalComment">/** Connection to the XMPP server */</FONT>
<A NAME="119"></A>    <FONT ID="Private">private</FONT> XMPPConnection connection;
<A NAME="120"></A>    <FONT ID="FormalComment">/** User name of the current connection (in form user@server - without the resource) */</FONT>
<A NAME="121"></A>    <FONT ID="Private">private</FONT> String userWithoutResource;
<A NAME="122"></A>    <FONT ID="FormalComment">/** The database connector class */</FONT>
<A NAME="123"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/DatabaseConnector.java.html">DatabaseConnector</A> theDBConnector;
<A NAME="124"></A>}
<A NAME="125"></A></pre>
</BODY>
</HTML>